 /* File di inserimento e gestione contenuti nalla sezione "Creazione Lezione" */    


	var _debug = true ;
	var myArray_items_s = {}; /* struttura dati contenente gli item da inserire ed eliminare */
	var counter_s = 0;        /* aggiornare con l'ultimo valore univoco (id) salvato + 1 */
    var current_position_s = -1;
    
	
    
    /***************************************************************************************************************************/
    /***************************************************************************************************************************/

    //                   GESTIONE AGGIUNTA, MODIFICA E CANCELLAZIONE DEGLI ITEM NELLA TAB SETTAGGIO

    /***************************************************************************************************************************/
    /***************************************************************************************************************************/
    
	//cancella un item al click sul bottone elimina
	function fmy_deleteItem_settaggio( p_position, p_name ) {
		if ( _debug ) console.log( "function name: " + "fmy_deleteItem_settaggio(p_position)" + "p_position: " + p_position );
		delete myArray_items_s[p_position];
		if ( _debug ) 
		{
			for (var elm in myArray_items_s) {
				console.log( "Value: "+elm+"\n");
			}
		}
		$("#itemAdded_"+ p_position).remove();
        
         if ( p_name == "autore" ){
            $( ".item_content_inside.info_autore" ).show("500");
            $( "#annulla_autore" ).css ( "display", "none" );
        }
        
	}
    
    
    //modifica un item al click sul bottone "modifica"
	function fmy_modificaItem_settaggio( p_position, p_name ) {
  
        fmy_fillDisplayItems( p_position, p_name  );
        
        current_position_s = p_position ;
	    
        
        if ( p_name == "autore" ) {
            $( ".items_toDisplay_autore" ).hide("500");
            $( ".item_content_inside.info_"+p_name ).slideDown( "slow" ) ;

            value_toModify = $( "#itemAdded_" + p_position ).text();
            $( ".item_button_inside" ).show();
            $( ".item_content_inside" ).find( "input#txt_" + p_name ).val( value_toModify );
            $( ".item_content_inside #aggiungi_" + p_name ).css( "display", "none" );
            $( ".item_content_inside #modifica_" + p_name ).css( "display", "block" );
            $( ".items_toDisplay_autore .current_autore" ).css( "display", "block" );
            
            
        } else {
            $( ".item_button_inside" ).hide( "slow" );
            $( ".item_content_inside.info_"+p_name).slideDown( "slow" ) ;

            value_toModify = $( "#itemAdded_" + p_position ).text();

            $( ".item_content_inside" ).find( "input#txt_" + p_name ).val( value_toModify );
            $( ".item_content_inside #aggiungi_" + p_name ).css( "display", "none" );
            $( ".item_content_inside #modifica_" + p_name ).css( "display", "block" );
            
        }
       
    }
    
    //aggiorna i campi del coautore al click sul bottone "matitina"
    function fmy_updateItem_settaggio( p_name ) {

        $( "#itemAdded_" + current_position_s ).find( ".name_item_added" ).text( value_toModify );
        
        tmp_name = $( ".item_content_inside" ).find( "input#name_" + p_name ).val();
        tmp_surname = $( ".item_content_inside" ).find( "input#surname_" + p_name ).val();

        
        if ( p_name == "autore" ){
           
            if ( tmp_name.length > 0 &&  tmp_surname.length > 0 ) {
                
                fmy_fillArrayItems( p_name );
                
                $( ".item_content_inside.info_"+p_name ).slideUp( "slow" ) ;
                $( ".item_content_inside #aggiungi_" + p_name ).css( "display", "block" );
                $( ".item_content_inside #modifica_" + p_name ).css( "display", "none" );
                       
                strToAppend = tmp_name + " " +tmp_surname;
                $( ".items_toDisplay_"+p_name ).find( "#itemAdded_"+current_position_s +" .name_item_added" ).val( strToAppend );
                $( ".items_toDisplay_autore" ).show("500");
                $( ".items_toDisplay_autore .current_autore" ).css( "display", "block" );
             }
             
        } else {
            
            if ( tmp_name.length > 0 &&  tmp_surname.length > 0 ) {
                
                fmy_fillArrayItems( p_name );
                
                $( ".item_content_inside.info_"+p_name ).slideUp( "slow" ) ;
                $( ".item_content_inside #aggiungi_" + p_name ).css( "display", "block" );
                $( ".item_content_inside #modifica_" + p_name ).css( "display", "none" );
                $( ".item_button_inside" ).hide( "slow" );
               
                strToAppend = tmp_name + " " +tmp_surname;
                $( ".items_toDisplay_"+p_name ).find( "#itemAdded_"+current_position_s +" .name_item_added" ).val( strToAppend );
                
            }
        }
        
        fmy_cleanInputs( p_name );

    }
	   
    
    //riempie l'array con i valori scritti nell'input area
    function fmy_fillArrayItems( p_name ) {
        myArray_items_s[current_position_s][0] = $( ".item_content_inside" ).find( "input#name_" + p_name ).val();
        myArray_items_s[current_position_s][1] = $( ".item_content_inside" ).find( "input#surname_" + p_name ).val();
        myArray_items_s[current_position_s][2] = $( ".item_content_inside" ).find( "input#job_" + p_name ).val();
        myArray_items_s[current_position_s][3] = $( ".item_content_inside" ).find( "input#mail_" + p_name ).val();
        myArray_items_s[current_position_s][4] = $( ".item_content_inside" ).find( "input#photo_path_" + p_name ).attr("src");
        myArray_items_s[current_position_s][5] = $( ".item_content_inside" ).find( "textarea#life_textarea_" + p_name ).val();
     }
    
    //riempie gli input area con i valori dell'array per la conseguente visualizzazione nel form
     function fmy_fillDisplayItems( p_position, p_name ) {
        $( ".item_content_inside" ).find( "input#name_" + p_name ).val( myArray_items_s[p_position][0] );
        $( ".item_content_inside" ).find( "input#surname_" + p_name ).val( myArray_items_s[p_position][1] );
        $( ".item_content_inside" ).find( "input#job_" + p_name ).val( myArray_items_s[p_position][2] );
        $( ".item_content_inside" ).find( "input#mail_" + p_name ).val( myArray_items_s[p_position][3] );
        $( ".item_content_inside" ).find( "input#photo_path_" + p_name ).attr("src", myArray_items_s[p_position][4] );
        $( ".item_content_inside" ).find( "textarea#life_textarea_" + p_name ).val( myArray_items_s[p_position][5] );

     }
    
	//stampa un item aggiunto nel div items
	function fmy_printItem_settaggio( p_itemArray, p_name ) {
		if ( _debug ) console.log( "function name: " + "fmy_printItem_settaggio ," + "p_itemArray: " + p_itemArray[0] );
        
        
		myArray_items_s[counter_s] = p_itemArray ;
		var last_position = counter_s;
		if ( _debug ) console.log( "last_position nell'array: " + last_position );

        strToAppend =  "<div ";
        strToAppend += "class=\"item_added\" ";
        strToAppend += "id=\"itemAdded_"+ last_position;
        strToAppend += "\" >";
        
        // input nome elemento inserito
        strToAppend += "<input type=\"text\" ";
        strToAppend += "value=\""+ myArray_items_s[last_position][0] + " " +myArray_items_s[last_position][1] +"\" ";
        strToAppend += "class=\"name_item_added\" ";
        strToAppend += "name=\"dettagli["+counter_s+"].descrizione\" ";        
        strToAppend += "/> ";
        
        
        // input cancella
        strToAppend += "<input type=\"button\" ";
        strToAppend += "value=\"\" ";
        strToAppend += "class=\"btn_delete\" ";
        strToAppend += "id=\"delete_"+ last_position +"\" ";
        strToAppend += "onclick=\"fmy_deleteItem_settaggio("+ last_position + ",'"+ p_name + "');\"/> ";
        
        // input ordinamento
        /*
        strToAppend += "<input type=\"button\" ";
        strToAppend += "value=\"\" ";
        strToAppend += "class=\"btn_ordina\" ";
        strToAppend += "id=\"ordina_"+ last_position +"\" ";
        strToAppend += "onclick=\"fmy_ordinaItem_settaggio("+ last_position + ");\"/> ";
        */
       
        // input modifica
        strToAppend += "<input type=\"button\" ";
        strToAppend += "value=\"\" ";
        strToAppend += "class=\"btn_modifica\" ";
        strToAppend += "id=\"modifica_"+ last_position +"\" ";
        strToAppend += "onclick=\"fmy_modificaItem_settaggio("+ last_position + ",'"+ p_name + "');\"/> ";
        
        strToAppend += "</div>";
		
        $( ".items_toDisplay_" + p_name ).append( strToAppend );
        counter_s ++ ;
    
    }
	
	//aggiunge un obiettivo al click sul bottone aggiungi e gli assegna la funzione sort
	function fmy_addItem_settaggio ( p_name ) {
		if ( _debug ) console.log( "function name: " + "fmy_addItem_settaggio" );
		
        var my_name = "";
		my_name = $( "#name_"+ p_name ).val();
        
        var my_surname = "";
		my_surname = $( "#surname_"+ p_name ).val();
        
        var my_job = "";
		my_job = $( "#job_"+ p_name ).val();
        
        var my_mail = "";
		my_mail = $( "#mail_"+ p_name ).val();
        
        var my_photo_path = "";
		my_photo_path = $( "#photo_path_"+ p_name ).attr("src");

        var my_life_textarea = "";
		my_life_textarea = $( "#life_textarea_"+ p_name ).val();
                
        
		if ( _debug ) console.log( "my_name         : " + my_name );
        if ( _debug ) console.log( "my_surname      : " + my_surname );
        if ( _debug ) console.log( "my_job          : " + my_job);
        if ( _debug ) console.log( "my_mail         : " + my_mail );
        if ( _debug ) console.log( "my_photo_path   : " + my_photo_path );
        if ( _debug ) console.log( "my_life_textarea: " + my_life_textarea );

        
        itemArray = [];
        itemArray[0] = my_name;
        itemArray[1] = my_surname;
        itemArray[2] = my_job;
        itemArray[3] = my_mail;
        itemArray[4] = my_photo_path;
        itemArray[5] = my_life_textarea;
        
        //disegna il box se sono stati inseriti nome e cognome
        if ( itemArray[0].length > 0 &&  itemArray[1].length > 0 ) {
            fmy_printItem_settaggio( itemArray, p_name );
            if ( p_name == "autore" ){
                $( ".item_content_inside.info_"+p_name).slideUp( "slow" ) ;
                $( ".items_toDisplay_autore" ).show("500");
                $( "#annulla_autore" ).css ( "display", "block" );
                $( ".items_toDisplay_autore .current_autore" ).css( "display", "block" );
            }
            fmy_cleanInputs( p_name );
        }
        
	}
    
   
   /* pulisce  i campi del form */
    function fmy_cleanInputs( p_name ) {
        $( "#name_"+ p_name ).val('');
        $( "#surname_"+ p_name ).val('');
        $( "#job_"+ p_name ).val('');
        $( "#mail_"+ p_name ).val('');
        $( "#photo_path_"+ p_name ).val('');
        $( "#life_textarea_"+ p_name ).val('');
    }
    
    
    function fmy_cancelItem( p_name ) {
         fmy_cleanInputs(p_name);        
    }
   
    function getLastKey() {  

        var lastKey = 0;
        $.each(myArray_items_s, function(key, value) {
              lastKey = key; 
        });
         
        //console.log("lastKey: "+lastKey);
        return lastKey;
    } 
    
    
    /***************************************************************************************************************************/
    /***************************************************************************************************************************/

    //                   GESTIONE RADIO BUTTON NELLA TAB SETTAGGIO

    /***************************************************************************************************************************/
    /***************************************************************************************************************************/


    function fmy_ifCheckedThenDisplay ( p_className ) {
        
        if ( $( p_className ).find( "input#prezzo_2[type='radio']" ).is(":checked") ) {
            $( p_className ).find( ".contenuto_radio2" ).show();
        }
        else {
            $( p_className ).find( ".contenuto_radio2" ).hide();
        }
        
    }


    function fmy_checkRadio( p_className ) {
        
        
        $( p_className ).find( ".left_radio" ).live("click", function () {
            $( p_className ).find( ".contenuto_radio1" ).show( "500" );
            $( p_className ).find( ".contenuto_radio2" ).hide( "500" );
        });

        $( p_className ).find( ".right_radio" ).live("click", function () {
            $( p_className ).find( ".contenuto_radio2" ).show( "500" );
            $( p_className ).find( ".contenuto_radio1" ).hide( "500" );
            
           
        });

        $( p_className ).find( ".left_radio_inside" ).live("click", function () {
            $( p_className ).find( ".contenuto_radio1_inside" ).show( "500" );
            $( p_className ).find( ".contenuto_radio2_inside" ).hide( "500" );
        });

        $( p_className ).find( ".right_radio_inside" ).live("click", function () {
            $( p_className ).find( ".contenuto_radio2_inside" ).show( "500" );
            $( p_className ).find( ".contenuto_radio1_inside" ).hide( "500" );
        });
        
        
    }
    
    
    
    
    
       
    
    
    /***************************************************************************************************************************/
    /***************************************************************************************************************************/

    //                   GESTIONE "AGGIUNGI Co-autore" NELLA TAB SETTAGGIO

    /***************************************************************************************************************************/
    /***************************************************************************************************************************/
    
    
    /*---- BEGIN ANIMAZIONE SLIDING CREA CORSO ----*/
    function animateOpenBlock() {
        $( ".close_item_inside" ).live("click", function () {
            $( this ).next().slideDown( "500" );
            $( this ).slideUp( "500" );
            $( this ).removeClass( "close_item_inside" ).addClass( "open_item_inside" );
        });    
    }


    function testonclick() {  
        $( ".sez_autore .accordion_close_btn#annulla_autore" ).live( "click" , function () {
            $( ".item_content_inside.info_autore" ).hide("500") ;
            $( ".items_toDisplay_autore" ).show("500") ;
        }); 
        $( ".accordion_close_btn" ).live( "click" , function () {
            //var itemBtn = $(this).parent().parent().parent().prev();
            var itemBtn = $(this).closest( ".item_content_inside" ).prev();
            itemBtn.next().slideUp( "500" );
            itemBtn.slideDown( "500" );
            itemBtn.removeClass( "open_item_inside" ).addClass( "close_item_inside" );
        }); 
    }

    /*---- END ANIMAZIONE SLIDING CREA CORSO ----*/
    
 
        
    /***************************************************************************************************************************/
    /***************************************************************************************************************************/

    //                   DOCUMENT READY

    /***************************************************************************************************************************/
    /***************************************************************************************************************************/
    

    
    $(document).ready(function(){
       
        //gestione animazione radio button 
        fmy_checkRadio(".switch_radio_prezzo"); //settaggio -> prezzo
        fmy_checkRadio(".switch_radio_autori"); //settaggio -> autori
        
        fmy_ifCheckedThenDisplay( ".switch_radio_prezzo" );
        
        //gestiscono l'apertura del blocco al click su aggiungi co-autore
        animateOpenBlock();
        testonclick();
        
    });